package bit;

/**
 * @author cxx
 * @create 2020-04-29 14:32
 **/
public class BitMap {

    private char[] bytes;
    private int nbits;

    public BitMap(int nbits) {
        this.nbits = nbits;
        this.bytes = new char[nbits/8+1];
    }

    public void set(int k) {
        if (k > nbits) return;
        int byteIndex = k / 8;
        int bitIndex = k % 8;
        bytes[byteIndex] |= (1 << bitIndex);
    }

    public boolean get(int k) {
        if (k > nbits) return false;
        int byteIndex = k / 8;
        int bitIndex = k % 8;
        return (bytes[byteIndex] & (1 << bitIndex)) != 0;
    }

    public static void main(String[] args) {
//        BitMap bitMap = new BitMap (100000);
//        bitMap.set (90);
//        bitMap.set (89);
//        System.out.println (bitMap.get (90));
//        System.out.println (bitMap.get (89));
    }

}
